<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Parser extends CI_Parser {
        
        /**
         * Extended Template Parser
         * @version 1.1
         * @author Bit0reZ (b1t0r3z@gmail.com)
         * @license GNU GPL v3
         * @copyright Bit0reZ
         */

    public function __construct()
    {
        //parent::__construct();
    }
    
    private function inner($db)
    {
        $data = array();
        $j = 0;
        while(isset($db[$j]))
        {
                foreach($db[$j] as $key => $val)
                {      
                        $data[$j][$key.':i'] = $val;
                }
                $j++;
        }
        return $data;
    }

    public function extparser($template, $data , $return = FALSE)
    {
        $CI =& get_instance();
	$template = $CI->load->view($template, $data, TRUE);
        $toTplData = $data;
        preg_match_all('|'.preg_quote($this->l_delim).'(.+?)'.preg_quote($this->r_delim).'|', $template, $matchesarray);
        $i = 0;
        while(isset($matchesarray[1][$i]))
        {      
                if(!isset($data[$matchesarray[1][$i]]) && '/' !== substr($matchesarray[1][$i], 0, 1))
                {
                        if(preg_match("/{/", $matchesarray[1][$i], $match)) $data[$matchesarray[1][$i]] = $matchesarray[1][$i];
                        else if(preg_match("/[^\s]+?_block(?=\:b)/", $matchesarray[1][$i], $match)) $data[$matchesarray[1][$i]] = $this->extparser(TPLPATH.'blocks/'.$match[0].TPLEXT, $toTplData, true);
                        else if(preg_match("/[^\s]+(?=\:b)/", $matchesarray[1][$i], $match)) $data[$matchesarray[1][$i]] = $this->extparser(TPLPATH.'core/'.$match[0].TPLEXT, $toTplData, true);
                        else if(preg_match("/[^\s]+(?=\:m(?!\:\d+))/", $matchesarray[1][$i], $match))
                        {
                                $tmp = $CI->model->$match[0]();
                                if(!is_array($tmp))
                                        $data[$matchesarray[1][$i]] = $tmp;
                                else
                                {
                                        $data[$matchesarray[1][$i]] = $this->inner($tmp);
                                        $i = array_search('/'.$matchesarray[1][$i], $matchesarray[1]);
                                }
                        }
                        else if(preg_match("/[^\s]+(?=\:m\:(\d+)$)/", $matchesarray[1][$i], $match))
                        {
                                $data[$matchesarray[1][$i]] = array();
                                $tmp = $CI->model->$match[0]($match[1]);
                                if(is_array($tmp))
                                {
                                        $data[$matchesarray[1][$i]] = $this->inner($tmp);
                                        $i = array_search('/'.$matchesarray[1][$i], $matchesarray[1]);
                                }
                                else
                                        $data[$matchesarray[1][$i]] = $tmp;
                        }
                        else if(preg_match("/[^\s]+(?=\:m\:(\d+)([\:\w]+))/", $matchesarray[1][$i], $match))
                        {
                                $data[$matchesarray[1][$i]] = array();
                                preg_match_all("/(?<=\:)\w+/", $match[2], $mch);
                                //foreach($mch as $val) echo $val.'<br />';
                                //print_r($mch);
                                $tmp = $CI->model->$match[0]($match[1]);
                                $data[$matchesarray[1][$i]] = $this->inner($tmp);
                                $i = array_search('/'.$matchesarray[1][$i], $matchesarray[1]);
                        }
                        else if(preg_match("/[^\s]+(?=\:c(?=\:?(\w*)))/", $matchesarray[1][$i], $match))
                        {
                                $tmp = $CI->class->$match[0]();
                                if(!is_array($tmp))
                                        $data[$matchesarray[1][$i]] = $tmp;
                                else
                                {
                                        $data[$matchesarray[1][$i]] = $this->inner($tmp);
                                        $i = array_search('/'.$matchesarray[1][$i], $matchesarray[1]);
                                }
                        }
                        else
                        {
                                $data[$matchesarray[1][$i]] = $matchesarray[1][$i]();
                        }
                }
                else if(is_array($data[$matchesarray[1][$i]])) $i = array_search('/'.$matchesarray[1][$i], $matchesarray[1]);
                $i++;
        }
	return $this->_parse($template, $data, $return);
    }
}